<template>
    <div class="auth-container">
        <div class="register">
            <h2>注册</h2>
            <form class="auth-form" @submit.prevent="register">
                <input v-model="username" type="text" placeholder="用户名" required />
                <input v-model="password" type="password" placeholder="密码" required />
                <input v-model="email" type="email" placeholder="邮箱" required @blur="validateEmail" />
                <span v-if="emailError" class="error">{{ emailError }}</span>
                <div class="verification-section">
                    <input v-model="verificationCode" type="text" placeholder="验证码" required />
                    <button type="button" @click="sendVerificationCode" :disabled="isCountingDown">
                        {{ isCountingDown ? countdown : '获取验证码' }}
                    </button>
                </div>
                <button type="submit" :disabled="!canRegister">注册</button>
            </form>
            <div class="return-login">
                <router-link to="/login">返回登录</router-link>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import { post } from "../until/request";
import { useRouter } from 'vue-router';

const router = useRouter();  // 路由
const username = ref('');   // 用户名
const email = ref(''); // 邮箱
const password = ref(''); // 密码
const verificationCode = ref('');   // 输入的验证码
const emailError = ref('');     // 邮箱错误
const countdown = ref(60);      // 倒计时
const isCountingDown = ref(false); // 是否在倒计时
let countdownInterval = null; // 倒计时间隔

// 验证邮箱
function validateEmail() {
    const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    emailError.value = emailPattern.test(email.value) ? '' : '请输入有效的邮箱地址';
}

// 发送验证码
function sendVerificationCode() {
    if (emailError.value) return;

    // 开始倒计时
    isCountingDown.value = true;
    countdown.value = 60;
    countdownInterval = setInterval(() => {
        if (countdown.value > 0) {
            countdown.value -= 1;
        } else {
            clearInterval(countdownInterval);
            isCountingDown.value = false;
        }
    }, 1000);

    // 发送验证码逻辑
    post("/api/emailVerification", {
        email: email.value
    }).then((res) => {
        if (res.code === 1) {
            console.log(res);
        }
    });
    console.log('验证码已发送到邮箱:', email.value);
}

// 判断是否可以注册
const canRegister = computed(() => {
    return username.value && password.value && email.value && verificationCode.value && !emailError.value;
});

// 注册
function register() {
    if (canRegister.value) {
        // 注册逻辑
        post("/api/register", {
            username: username.value,
            password: password.value,
            email: email.value,
            captcha: verificationCode.value
        }).then((res) => {
            if (res.code === 1) {
                console.log(res);
                alert('注册成功');
                router.push({ name: "Login" });
            }
        });
    }
}
</script>

<style lang="scss" scoped>
.auth-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f0f2f5;
}

.register {
    background-color: #fff;
    padding: 2rem;
    border-radius: 8px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    width: 300px;
    text-align: center;
}

.auth-form {
    display: flex;
    flex-direction: column;
}

.auth-form input {
    margin-bottom: 10px;
    padding: 10px;
    border: 1px solid #ccc;
    border-radius: 5px;
}

.verification-section {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
}

.verification-section input {
    flex: 1;
    margin-right: 10px;
}

.auth-form button {
    padding: 10px;
    background-color: #007bff;
    color: #fff;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    transition: background-color 0.3s;
    margin-bottom: 10px;
}

.auth-form button:disabled {
    background-color: #ccc;
    cursor: not-allowed;
}

.auth-form button:hover:not(:disabled) {
    background-color: #0056b3;
}

.error {
    color: red;
    font-size: 0.8em;
    margin-bottom: 10px;
}

.return-login {
    font-size: 15px;
    margin-top: 20px;
    text-align: left;
}

.return-login a {
    color: #007bff;
    text-decoration: none;
    transition: color 0.3s;
}

.return-login a:hover {
    color: #0056b3;
}
</style>
